/**
 * 首页接口
 * @author 梁国玺
 * @Date 2023-09-20 13:32:46
 */
import {queryVideoList, channelList, commentList} from '@/api/share'
import home from "../../views/home";
import {addVisit} from "../../api/share";
import {Toast} from "vant";
const state = {
    channels: [],//导航栏菜单
    videoList: [],//视频列表
    videoItem: {}, //存放当前展示的视频
    commentList: [],//当前视频评论列表
    videoId: ''
}
const getters = {}
const mutations = {
    SET_VODIE_ID: (state, id) => {
        state.videoId = id
    },
    addCollectNum: (state) => {
        state.videoItem.collectNum += 1
    },
    reduceCollectNum: (state) => {
        state.videoItem.collectNum -= 1
    },
    addLikesNum: (state) => {
        state.videoItem.likeNum += 1
    },
    reduceLikesNum: (state) => {
        state.videoItem.likeNum -= 1
    },
    SET_CHANNELS: (state, channels) => {
        console.log('channel', channels)
        state.channels = channels
    },
    SET_VIDEOS: (state, videos) => {
        state.videoList = videos
    },
    UPDATE_VIDES_LIST: (state, id) => {
        const itemIndex = state.videoList.findIndex(item => item.id === id);
        if (itemIndex !== -1) {
            console.log('找到该视频')
            const item = state.videoList.splice(itemIndex, 1)[0];
            state.videoList.unshift(item);
            console.log('换位置之后的videoList', state.videoList)
        }
        console.log('未找到该视频', itemIndex)
    },
    SET_VIDEO_ITEM: (state, item) => {
        console.log('share.js 切换每个视频', item)
        state.videoItem = item
    },
    SET_COMMENT_LIST: (state, remarks) => {
        console.log('share.js commentlist', remarks)
        state.commentList = remarks
    },
    UPDATE_COMMENT_LIST: (state, comment) => {
        // console.log('--------------------',comment)
        // console.log('+++++++++++++++',state.commentList)
        if (comment.reply) {
            state.commentList.forEach((item, index) => {
                if (item.id == comment.reply) {
                    state.commentList[index].children.push(comment)
                } else {
                    if (state.commentList[index].children) {
                        let findindex = state.commentList[index].children.findIndex(item => item.id == comment.reply)
                        state.commentList[index].children.forEach((itemChildren, indexChildren) => {
                            console.log(findindex)
                            if (itemChildren.id == comment.reply) {
                                state.commentList[index].children.push(comment)
                            }
                        })
                    }
                }
            })
        } else {
            state.commentList.push(comment)
        }
    },
    DELETE_COMMENT_LIST: (state, comment) => {
        state.commentList.forEach((item, index) => {
            if (item.id == comment.id) {
                state.commentList.splice(index, 1)
            } else {
                if (state.commentList[index].children) {
                    state.commentList[index].children.forEach((itemChildren, indexChildren) => {
                        if (itemChildren.id == comment.id) {
                            state.commentList[index].children.splice(indexChildren, 1)
                        }
                    })
                }
            }
        })
    }

}
const actions = {
    setVideoItem({commit}, item) {
        commit('SET_VIDEO_ITEM', item)
    },
    async getChannelLlist({commit}, params) {
        let res = await channelList(params)
        // console.log(res.data)
        commit('SET_CHANNELS', res.data)
    },
    async getVideoList({commit}, params) {
        let res = await queryVideoList(params)
        if (res.data.records.length > 0) {
            // res.data.records.push(...res.data.records)
            commit('SET_VIDEOS', res.data.records)
        } else {
           Toast.fail('暂无视频内容')
        }
        if (state.videoId) {
            console.log('有video_id', state.videoId)
            commit('UPDATE_VIDES_LIST', state.videoId)
        }
        commit('SET_VIDEO_ITEM', res.data.records[0])
    },
    // async getCommentList({commit}, id) {
    //     let res = await commentList(id)
    //     commit('SET_COMMENT_LIST', res.data)
    // }
}
export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}